function [  ] = segmentPNNew( inputFileName,outputFileName)
%this function try to segment PN toumers
    
    origImage = load_nifti(inputFileName);
    
    %perfore some light morphology in order to fill little holes
    se1 = strel('disk',4);
    origImage = imdilate(origImage,se1);
    origImage = imerode(origImage,se1);
    
    
    
    segMatrix = zeros(size(origImage));
    [h w d] = size(origImage);
    
    %normalize and strerch image's values to 0-1 range
    image = NormalizeAndstretchImage(origImage);
    
    %perform histogram equalization
    vec = reshape(image,1,h*w*d);
    vec = histeq(vec);
    image = reshape(vec,h,w,d);
    
    %calculate intersity threshhold
    heighVals = (vec(vec>0.9));
    intensity_TH = mean(heighVals) + 4*var(heighVals);
    
    %prepare the initial segmentation matrix
    initialSeeds = zeros(h,w,d);
    initialSeeds(image>intensity_TH) = 1;
    
    %go over all connected component and check the ratio: surface/volume
    CC = bwconncomp(initialSeeds,26);
    
    numberOfCC = CC.NumObjects;
    
    for i=1:numberOfCC
        
        %ignore small areas
        if (length(CC.PixelIdxList{i})<400)
            continue;
        end;
        
        SegCC = zeros(size(image));
        SegCC(CC.PixelIdxList{i})=1;
        
        CCVol = length(CC.PixelIdxList{i});
        
        CCSmall = imerode(SegCC,strel('disk',1));
        CCSurface = SegCC-CCSmall;
        %check the ration surface/vol of the current ConnectedComponent
        if (sum(CCSurface(:))/CCVol > 0.4)
            continue;
        end;
           
        segMatrix(CC.PixelIdxList{i})=1;
    end

   save_nifti(segMatrix,outputFileName,2);

end


